import delimited "${datadir_hpindex}\UK-HPI-full-file-2020-08.csv", clear 

ren v8 appreciation1

gen dated = date(date, "DMY")
gen datem = mofd(dated)

encode areacode, gen(coden)
xtset coden datem

gen appreciation2 = index/l24.index -1
gen appreciation3 = index/l36.index -1
gen appreciation4 = index/l48.index -1
gen appreciation5 = index/l60.index -1

keep datem areacode regionname appreciation* index

foreach i in  1 2 3 5 {
	bysort regionname: egen vol`i' = sd(appreciation`i')
}

save "${datadir}\local_price_appreciation_2020", replace


keep if month(dofm(datem)) == 12

// all years
keep areacode index regionname appreciation1 datem
bysort areacode: egen vol_allyear = sd(appreciation1)

// up to 2012
gen year = year(dofm(datem))
bysort areacode: egen vol_2012 = sd(appreciation1) if year<2013
bysort areacode (vol_2012): replace vol_2012 = vol_2012[1] if missing(vol_2012) 

// between 2006 and 2012, included
bysort areacode: egen vol_0612 = sd(appreciation1) if year>2005 & year<2013
bysort areacode (vol_0612): replace vol_0612 = vol_0612[1] if missing(vol_0612) 

keep areacode regionname vol_allyear vol_2012 vol_0612
duplicates drop
label data "From 01_prepare_local_price_appreciation_2020.do"
save "${datadir}\local_price_vol_2020", replace
